package web.manager;

import bean.Manager;
import bean.Staff;
import bean.Video;
import com.alibaba.fastjson.JSON;
import entity.ManagerEntity;
import entity.StaffEntity;
import entity.VideoEntity;
import util.FANREN;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "LocalLogin", urlPatterns = "/dao/update/login")
public class LocalLogin extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String jsonString = FANREN.getJson(request);
        System.out.println(jsonString);
        EntityManager em = (EntityManager) request.getAttribute("_entity.Manager");

        Manager manager = JSON.parseObject(jsonString,Manager.class);

        Query query = em.createNativeQuery("select * from ManagerEntity where name = ? and pwd = ?");
        query.setParameter(1, manager.getName());
        query.setParameter(2, manager.getPwd());
        List<Object[]> list = query.getResultList();

        if(list.size() == 0) {
            throw new RuntimeException();
        } else {
            manager.setMid((int) list.get(0)[0]);
            manager.setUsername((String) list.get(0)[3]);
            System.out.println(manager);
            request.getSession().setAttribute("manager",manager);
        }
    }
}
